package com.egoo.ticket.server.utils.common;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.collect.Lists;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Administrator
 * @Auther: victor_tang
 * @Date: 2020/4/21 13:16
 * @Description: Excel工具类
 */
public class ExcelUtil {
	/**
	 * @Author
	 * @Description 导出excel 支持一张表导出多个sheet
	 * @Param OutputStream 输出流
	 * Map<String, List>  sheetName和每个sheet的数据
	 * ExcelTypeEnum 要导出的excel的类型 有ExcelTypeEnum.xls 和有ExcelTypeEnum.xlsx
	 */
	/**
	 * TODO 创建导出响应流
	 * @author: victor_tang
	 * @createtime: 2020/4/22 10:09
	 * @param response 响应
	 * @param sheetNameAndDateList 导出文件名和需要导出的数据
	 * @param type 导出的类型
	 * @param fileName 文件名
	 * @param exportClazz 导出实体类
	 * @return: void
	 */
	public static void createExcelStreamMutilByEaysExcel(HttpServletResponse response, Map<String, List<? extends BaseRowModel>> sheetNameAndDateList, ExcelTypeEnum type, String fileName, Class<? extends BaseRowModel> exportClazz) throws UnsupportedEncodingException {
		try {
			response.setContentType("multipart/form-data");
			response.setCharacterEncoding("utf-8");
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
			ServletOutputStream out = response.getOutputStream();
			ExcelWriter writer = new ExcelWriter(out, type, true);
			setSheet(sheetNameAndDateList, writer, exportClazz);
			writer.finish();
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * TODO 以当前时间生成文件名
	 * @author: victor_tang
	 * @createtime: 2020/4/21 13:20
	 * @param
	 * @return: java.lang.String
	 */
	public static String fileName() {
		String fileName = "";
		try {
			fileName = new String(
					(new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return fileName;
	}

	/**
	 * TODO setSheet数据（支持多sheet）
	 * @author: victor_tang
	 * @createtime: 2020/4/22 10:09
	 * @param sheetNameAndDateList 导出excel数据和sheet表名（多sheet时，map传多个即可）
	 * @param writer 2
	 * @return: void
	 */
	private static void setSheet(Map<String, List<? extends BaseRowModel>> sheetNameAndDateList, ExcelWriter writer,Class<? extends BaseRowModel> exportClazz ) {
		int sheetNum = 1;
		for (Map.Entry<String, List<? extends BaseRowModel>> stringListEntry : sheetNameAndDateList.entrySet()) {
			Sheet sheet= new Sheet(sheetNum, 0,exportClazz);
			sheet.setSheetName(stringListEntry.getKey());
			// 支持多sheet
			writer.write(stringListEntry.getValue(), sheet);
			sheetNum++;
		}
	}


	public static void ExcelStreamMutilByEaysExcel(HttpServletResponse response, List<String> headsList,List<List<Object>> dataList, String fileName) throws UnsupportedEncodingException {
		try {
			response.setContentType("multipart/form-data");
			response.setCharacterEncoding("utf-8");
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
			ServletOutputStream out = response.getOutputStream();
			ExcelWriter excelWriter = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
			Sheet sheet = new Sheet(1,0);
			sheet.setSheetName("导出数据信息");
			// 创建一个表格
			Table table = new Table(1);
			List<List<String>> headList = new ArrayList<List<String>>();
			//第一行
			headsList.forEach(n->{
            	List<String> tempList=Lists.newArrayList();
            	tempList.add(n);
            	headList.add(tempList);
			});
			table.setHead(headList);
			excelWriter.write1(dataList,sheet,table);
			excelWriter.finish();
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
